package com.hc.tenant.service.impl;

import com.hc.common.core.utils.DateUtils;
import com.hc.tenant.domain.GpsLocation;
import com.hc.tenant.mapper.GpsLocationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.hc.tenant.mapper.RobotInfoMapper;
import com.hc.tenant.domain.RobotInfo;
import com.hc.tenant.service.IRobotInfoService;

import java.util.Date;

/**
 * 机器人运行信息Service业务层处理
 *
 * @author yjz
 * @date 2021-04-09
 */
@Service
public class RobotInfoServiceImpl implements IRobotInfoService
{
    @Autowired
    private RobotInfoMapper robotInfoMapper;

    @Autowired
    private GpsLocationMapper gpsLocationMapper;

    /**
     * 查询机器人运行信息
     *
     * @param userId 机器人运行信息ID
     * @return 机器人运行信息
     */
    @Override
    public RobotInfo selectRobotInfoByUserId(Long userId)
    {
        return robotInfoMapper.selectRobotInfoByUserId(userId);
    }

    /**
     * 新增机器人运行信息
     *
     * @param robotInfo 机器人运行信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertRobotInfo(RobotInfo robotInfo)
    {
        GpsLocation gpsLocation = robotInfo.getCurrentGpsLocation();
        gpsLocation.setCreateTime(new Date());
        gpsLocationMapper.insertGpsLocation(gpsLocation);
        robotInfo.setCurrentGpsLocationId(gpsLocation.getGpsLocationId());
        int rows = robotInfoMapper.insertRobotInfo(robotInfo);
        return rows;
    }

    @Transactional
    @Override
    public int updateRobotInfo(RobotInfo robotInfo)
    {
        GpsLocation gpsLocation = robotInfo.getCurrentGpsLocation();
        gpsLocation.setCreateTime(new Date());
        gpsLocationMapper.insertGpsLocation(gpsLocation);
        robotInfo.setCurrentGpsLocationId(gpsLocation.getGpsLocationId());
        robotInfo.setUpdateTime(DateUtils.getNowDate());
        return robotInfoMapper.updateRobotInfo(robotInfo);
    }


}